library(tidyverse) # for data cleaning and plotting
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.0.5 v dplyr 1.0.3
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(gardenR) # for Lisa's garden data
library(lubridate) # for date manipulation
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(openintro) # for the abbr2state() function
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(palmerpenguins)# for Palmer penguin data
library(maps) # for map data
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(ggmap) # for mapping points on maps
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(gplots) # for col2hex() function
##
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
##
## lowess
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(leaflet) # for highly customizable mapping
library(ggthemes) # for more themes (including theme_map())
library(plotly) # for the ggplotly() - basic interactivity
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggmap':
##
## wind
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(gganimate) # for adding animation layers to ggplots
library(gifski) # for creating the gif (don't need to load this library every time,but need it installed)
library(transformr) # for "tweening" (gganimate)
##
## Attaching package: 'transformr'
## The following object is masked from 'package:sf':
##
## st_normalize
library(shiny) # for creating interactive apps
library(patchwork) # for nicely combining ggplot2 graphs
library(gt) # for creating nice tables
##
## Attaching package: 'gt'
## The following object is masked from 'package:openintro':
##
## sp500
library(rvest) # for scraping data
## Loading required package: xml2
##
## Attaching package: 'rvest'
## The following object is masked from 'package:gt':
##
## html
## The following object is masked from 'package:purrr':
##
## pluck
## The following object is masked from 'package:readr':
##
## guess_encoding
library(robotstxt)# for checking if you can scrape data
library(paletteer)
## Warning: package 'paletteer' was built under R version 4.0.4
theme_set(theme_minimal())
# Lisa's garden data
data("garden_harvest")
#COVID-19 data from the New York Times
covid19 <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
##
## -- Column specification --------------------------------------------------------
## cols(
## date = col_date(format = ""),
## state = col_character(),
## fips = col_character(),
## cases = col_double(),
## deaths = col_double()
## )
Go here or to previous homework to remind yourself how to get set up.
Once your repository is created, you should always open your project rather than just opening an .Rmd file. You can do that by either clicking on the .Rproj file in your repository folder on your computer. Or, by going to the upper right hand corner in R Studio and clicking the arrow next to where it says Project: (None). You should see your project come up in that list if you’ve used it recently. You could also go to File –> Open Project and navigate to your .Rproj file.
Put your name at the top of the document.
For ALL graphs, you should include appropriate labels.
Feel free to change the default theme, which I currently have set to theme_minimal().
Use good coding practice. Read the short sections on good code with pipes and ggplot2. This is part of your grade!
NEW!! With animated graphs, add eval=FALSE to the code chunk that creates the animation and saves it using anim_save(). Add another code chunk to reread the gif back into the file. See the tutorial for help.
When you are finished with ALL the exercises, uncomment the options at the top so your document looks nicer. Don’t do it before then, or else you might miss some important warnings and messages.
shiny appapp.R file you create. Below, you will post a link to the app that you publish on shinyapps.io. You will create an app to compare states’ cumulative number of COVID cases over time. The x-axis will be number of days since 20+ cases and the y-axis will be cumulative cases on the log scale (scale_y_log10()). We use number of days since 20+ cases on the x-axis so we can make better comparisons of the curve trajectories. You will have an input box where the user can choose which states to compare (selectInput()) and have a submit button to click once the user has chosen all states they’re interested in comparing. The graph should display a different line for each state, with labels either on the graph or in a legend. Color can be used if needed.#I couldn't figure out how to get shinyapp to work so I just made the graph. Sorry
#I added a ggplotly so it's easier for you to tell which line is which
state_graph <- covid19 %>%
group_by(state, date, cases) %>%
filter(cases >= 20) %>%
group_by(state) %>%
mutate(min_month_date = min(date)) %>%
mutate(num_cases_since_20 = date - min_month_date) %>%
ggplot(aes(x = num_cases_since_20,
y = log10(cases),
color = state)) +
geom_line() +
labs(title = "Cumulative Covid19 Cases Over Time Per State",
x = "",
y = "")
ggplotly(state_graph)
## Don't know how to automatically pick scale for object of type difftime. Defaulting to continuous.
Raw button to get a direct link to the data.fake_garden_harv <- read.csv("https://raw.githubusercontent.com/llendway/scraping_etc/main/2020_harvest.csv")
groceries <- read.csv("Groceries_dataset.csv")
dim(groceries)
## [1] 38765 3
garden_harvest data as best as you can. When you get to coloring the cells, I used the following line of code for the colors argument:colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "RColorBrewer::YlGn"
) %>% as.character()
colors = scales::col_numeric(palette = paletteer::paletteer_d())
#{width=600, height=1000}
#for assigning colors
jul_colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "RColorBrewer::YlGn") %>%
as.character(),
domain = c(0.1, 13))
aug_colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "RColorBrewer::YlGn") %>%
as.character(),
domain = c(0.1, 32))
sep_colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "RColorBrewer::YlGn") %>%
as.character(),
domain = c(0.1, 28))
oct_colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "RColorBrewer::YlGn") %>%
as.character(),
domain = c(0.1, 12))
garden_harvest %>%
mutate(month = month(date, label = TRUE)) %>%
filter(vegetable %in% c("beans", "carrots", "tomatoes")) %>%
group_by(vegetable, variety, month) %>%
summarise(weight_lbs = sum(weight)*0.00220462) %>%
pivot_wider(id_cols = vegetable:weight_lbs,
names_from = "month",
values_from = "weight_lbs") %>%
replace_na(list(Jul = 0, Aug = 0, Sep = 0, Oct = 0)) %>%
gt(rowname_col = "variety",
groupname_col = "vegetable") %>%
tab_header(title = "Bean, Carrot, and Tomato Harvest (lb)") %>%
fmt_number(columns = c("Jul", "Aug", "Sep", "Oct"),
decimals = 2) %>%
summary_rows( #groups = TRUE,
columns = c("Jul", "Aug", "Sep", "Oct"),
fns = list(sum = "sum"),
formatter = fmt_number,
decimals = 1) %>%
data_color(columns = vars(Jul),
colors = jul_colors) %>%
data_color(columns = vars(Aug),
colors = aug_colors) %>%
data_color(columns = vars(Sep),
colors = sep_colors) %>%
data_color(columns = vars(Oct),
colors = oct_colors)
## `summarise()` has grouped output by 'vegetable', 'variety'. You can override using the `.groups` argument.
## Warning: Some values were outside the color scale and will be treated as NA
## Warning: Some values were outside the color scale and will be treated as NA
## Warning: Some values were outside the color scale and will be treated as NA
## Warning: Some values were outside the color scale and will be treated as NA
## Warning: The `.dots` argument of `group_by()` is deprecated as of dplyr 1.0.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
| Bean, Carrot, and Tomato Harvest (lb) | ||||
|---|---|---|---|---|
| Jul | Aug | Sep | Oct | |
| beans | ||||
| Bush Bush Slender | 12.89 | 9.04 | 0.21 | 0.00 |
| Chinese Red Noodle | 0.00 | 0.78 | 0.00 | 0.00 |
| Classic Slenderette | 0.00 | 3.07 | 0.53 | 0.00 |
| carrots | ||||
| Bolero | 0.26 | 3.37 | 1.36 | 3.31 |
| Dragon | 0.18 | 1.98 | 1.95 | 0.00 |
| greens | 0.00 | 0.37 | 0.00 | 0.00 |
| King Midas | 1.49 | 0.00 | 0.35 | 2.26 |
| tomatoes | ||||
| Amish Paste | 1.92 | 31.26 | 22.31 | 10.19 |
| Better Boy | 2.79 | 13.88 | 14.76 | 2.58 |
| Big Beef | 0.75 | 12.66 | 8.55 | 3.04 |
| Black Krim | 0.00 | 14.46 | 0.52 | 0.83 |
| Bonny Best | 1.72 | 15.84 | 6.51 | 0.85 |
| Brandywine | 0.00 | 11.81 | 2.91 | 0.92 |
| Cherokee Purple | 1.75 | 13.04 | 0.44 | 0.48 |
| grape | 1.34 | 15.87 | 12.15 | 3.04 |
| Jet Star | 0.69 | 7.60 | 5.96 | 0.76 |
| Mortgage Lifter | 1.77 | 12.14 | 9.05 | 3.36 |
| Old German | 2.74 | 9.25 | 10.88 | 3.85 |
| volunteers | 0.00 | 12.57 | 27.78 | 11.26 |
| sum | 30.3 | 189.0 | 126.2 | 46.7 |
gt with data from your project or from the garden_harvest data if your project data aren’t ready.garden_harvest_table <- garden_harvest %>%
filter(vegetable == "lettuce") %>%
gt(rowname_col = "variety",
groupname_col = "vegetable") %>%
tab_header(title = md("**Table of Lettuce From Garden Harvest Dataset**"))
garden_harvest_table
| Table of Lettuce From Garden Harvest Dataset | |||
|---|---|---|---|
| date | weight | units | |
| lettuce | |||
| reseed | 2020-06-06 | 20 | grams |
| reseed | 2020-06-08 | 15 | grams |
| reseed | 2020-06-09 | 10 | grams |
| Farmer's Market Blend | 2020-06-11 | 12 | grams |
| Farmer's Market Blend | 2020-06-13 | 19 | grams |
| Farmer's Market Blend | 2020-06-17 | 48 | grams |
| Farmer's Market Blend | 2020-06-18 | 47 | grams |
| Farmer's Market Blend | 2020-06-19 | 39 | grams |
| Farmer's Market Blend | 2020-06-19 | 38 | grams |
| Farmer's Market Blend | 2020-06-20 | 22 | grams |
| Tatsoi | 2020-06-20 | 18 | grams |
| Farmer's Market Blend | 2020-06-21 | 95 | grams |
| Farmer's Market Blend | 2020-06-22 | 52 | grams |
| Farmer's Market Blend | 2020-06-22 | 18 | grams |
| Farmer's Market Blend | 2020-06-24 | 122 | grams |
| Farmer's Market Blend | 2020-06-25 | 30 | grams |
| Farmer's Market Blend | 2020-06-27 | 52 | grams |
| Tatsoi | 2020-06-27 | 89 | grams |
| Farmer's Market Blend | 2020-06-28 | 111 | grams |
| Farmer's Market Blend | 2020-06-29 | 58 | grams |
| mustard greens | 2020-06-29 | 23 | grams |
| Farmer's Market Blend | 2020-06-29 | 82 | grams |
| Farmer's Market Blend | 2020-07-01 | 60 | grams |
| Tatsoi | 2020-07-02 | 144 | grams |
| Farmer's Market Blend | 2020-07-03 | 217 | grams |
| Tatsoi | 2020-07-03 | 216 | grams |
| Farmer's Market Blend | 2020-07-04 | 147 | grams |
| Tatsoi | 2020-07-06 | 189 | grams |
| Farmer's Market Blend | 2020-07-07 | 67 | grams |
| Farmer's Market Blend | 2020-07-07 | 13 | grams |
| Farmer's Market Blend | 2020-07-08 | 39 | grams |
| Tatsoi | 2020-07-08 | 75 | grams |
| Farmer's Market Blend | 2020-07-09 | 61 | grams |
| Farmer's Market Blend | 2020-07-11 | 79 | grams |
| Farmer's Market Blend | 2020-07-12 | 83 | grams |
| Farmer's Market Blend | 2020-07-13 | 53 | grams |
| Tatsoi | 2020-07-13 | 137 | grams |
| Farmer's Market Blend | 2020-07-16 | 61 | grams |
| Tatsoi | 2020-07-20 | 123 | grams |
| Lettuce Mixture | 2020-07-22 | 23 | grams |
| Lettuce Mixture | 2020-07-23 | 130 | grams |
| Lettuce Mixture | 2020-07-24 | 16 | grams |
| Lettuce Mixture | 2020-07-26 | 81 | grams |
| Lettuce Mixture | 2020-07-27 | 99 | grams |
| Lettuce Mixture | 2020-07-28 | 91 | grams |
| Lettuce Mixture | 2020-07-29 | 73 | grams |
| Lettuce Mixture | 2020-07-30 | 94 | grams |
| Lettuce Mixture | 2020-07-31 | 107 | grams |
| Lettuce Mixture | 2020-08-03 | 65 | grams |
| Lettuce Mixture | 2020-08-04 | 56 | grams |
| Lettuce Mixture | 2020-08-06 | 98 | grams |
| Lettuce Mixture | 2020-08-07 | 19 | grams |
| Lettuce Mixture | 2020-08-11 | 92 | grams |
| Lettuce Mixture | 2020-08-12 | 73 | grams |
| Lettuce Mixture | 2020-08-14 | 80 | grams |
| Lettuce Mixture | 2020-08-15 | 56 | grams |
| Lettuce Mixture | 2020-08-16 | 45 | grams |
| Lettuce Mixture | 2020-08-17 | 67 | grams |
| Lettuce Mixture | 2020-08-18 | 87 | grams |
| Lettuce Mixture | 2020-08-20 | 99 | grams |
| Tatsoi | 2020-08-20 | 322 | grams |
| Lettuce Mixture | 2020-08-23 | 111 | grams |
| Lettuce Mixture | 2020-08-24 | 134 | grams |
| Lettuce Mixture | 2020-08-27 | 14 | grams |
| Lettuce Mixture | 2020-08-28 | 85 | grams |
| Lettuce Mixture | 2020-09-16 | 8 | grams |
| Lettuce Mixture | 2020-09-26 | 95 | grams |
| Lettuce Mixture | 2020-09-27 | 139 | grams |
| Lettuce Mixture | 2020-10-07 | 17 | grams |
patchwork operators and functions to combine at least two graphs using your project data or garden_harvest data if your project data aren’t read.g1 <- garden_harvest %>%
filter(vegetable == "radish") %>%
ggplot(aes(x = date,
y = weight)) +
geom_line() +
labs(title = "Weight of Radishes Over Time",
x = "",
y = "")
g2 <- garden_harvest %>%
filter(vegetable == "beets") %>%
ggplot(aes(x = date,
y = weight)) +
geom_line() +
labs(title = "Weight of Beets Over Time",
x = "",
y = "")
g1 + g2
DID YOU REMEMBER TO UNCOMMENT THE OPTIONS AT THE TOP?